package com.scmt.hmzd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.scmt.base.utils.BASE64DecodedMultipartFile;
import com.scmt.core.common.exception.ScmtException;
import com.scmt.core.common.utils.ResultUtil;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.Result;
import com.scmt.hmzd.entity.HmOrder;
import com.scmt.hmzd.entity.HmOrderImg;
import com.scmt.hmzd.entity.HmTechnician;
import com.scmt.hmzd.enums.OrderEnum;
import com.scmt.hmzd.service.IHmOrderImgService;
import com.scmt.hmzd.service.IHmOrderService;
import com.scmt.hmzd.service.IHmTechnicianService;
import com.scmt.hmzd.utils.UploadFileUtils;
import com.scmt.hmzd.wxLoginUtils.WxSecurityUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@Api(tags = "技师端接口")
@RestController
@RequestMapping("/scmt/appMain")
@Slf4j
public class APPTechnicianController {

    @Autowired
    private WxSecurityUtil securityUtil;
    @Autowired
    private IHmOrderService iHmOrderService;
    @Autowired
    private IHmOrderImgService iHmOrderImgService;
    @Autowired
    private IHmTechnicianService iHmTechnicianService;

    @GetMapping("queryMyOrderList")
    public Result<Object> queryMyOrderList(HmOrder hmOrder, PageVo pageVo) {
        try {
            String id = securityUtil.getCurrentWxUser().getId();
            QueryWrapper<HmTechnician> hmTechnicianQueryWrapper = new QueryWrapper<>();
            hmTechnicianQueryWrapper.lambda().eq(HmTechnician::getWxUserId, id);
            HmTechnician hmTechnician = iHmTechnicianService.getOne(hmTechnicianQueryWrapper);
            if(hmTechnician == null) {
                return ResultUtil.error("当前技师信息获取失败！请刷新重试！");
            }
            hmOrder.setTechnicianId(hmTechnician.getId());
            IPage<HmOrder> result = iHmOrderService.queryOrderListDataByPage(hmOrder, pageVo);
            return ResultUtil.data(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("查询异常:" + e.getMessage());
        }
    }

    @GetMapping("changeOrderStatus")
    public Result<Object> changeOrderStatus(@RequestParam String orderId, @RequestParam Integer status) {
        String id = securityUtil.getCurrentWxUser().getId();
        QueryWrapper<HmTechnician> hmTechnicianQueryWrapper = new QueryWrapper<>();
        hmTechnicianQueryWrapper.lambda().eq(HmTechnician::getWxUserId, id);
        hmTechnicianQueryWrapper.select("id");
        HmTechnician hmTechnician = iHmTechnicianService.getOne(hmTechnicianQueryWrapper);
        if(hmTechnician == null){
            hmTechnician = new HmTechnician();
        }

        HmOrder updateOrder = new HmOrder();
        updateOrder.setId(orderId);
        if (status.intValue() == OrderEnum.ORDER_OPERATION.getIndex()) { //技师接单
            updateOrder.setDvyTime(new Date());
            hmTechnician.setIsWork(3);
        } else if (status.intValue() == OrderEnum.ORDER_WAY.getIndex()) { //技师路途中
            updateOrder.setOutTime(new Date());
            hmTechnician.setIsWork(3);
        } else if (status.intValue() == OrderEnum.ORDER_ARRIVES.getIndex()) { //技师到达
            updateOrder.setArrivalTime(new Date());
            hmTechnician.setIsWork(3);
        } else if (status.intValue() == OrderEnum.ORDER_SERVICE.getIndex()) { //开始服务
            updateOrder.setStartTime(new Date());
            hmTechnician.setIsWork(3);
        } else if (status.intValue() == OrderEnum.ORDER_STOP.getIndex()) { //服务结束
            updateOrder.setEndTime(new Date());
            hmTechnician.setIsWork(1);
        }
        updateOrder.setStatus(status);
        boolean b = iHmOrderService.updateById(updateOrder);
        if (b) {
            if(StringUtils.isNotBlank(hmTechnician.getId())) {
                iHmTechnicianService.updateById(hmTechnician); //修改技师的服务状态为服务中 还是服务结束
            }
            return ResultUtil.data("修改成功！");
        }
        throw new ScmtException("修改订单状态失败！");
    }

    @PostMapping("uploadImgFile")
    public Result<Object> uploadImgFile(@RequestBody HmOrderImg hmOrderImg) {
        //base64 转文件
        MultipartFile imgFile = BASE64DecodedMultipartFile.base64ToMultipart(hmOrderImg.getImgPath());
        //文件存储在nginx代理路径下
        String fileName = UploadFileUtils.uploadFile(imgFile);
        hmOrderImg.setImgPath(fileName);
        hmOrderImg.setCreateTime(new Date());
        if (hmOrderImg.getStatus() == OrderEnum.ORDER_ARRIVES.getIndex() - 1) {
            hmOrderImg.setType("技师到达");
        }
        if (hmOrderImg.getStatus() == OrderEnum.ORDER_SERVICE.getIndex() - 1) {
            hmOrderImg.setType("开始服务");
        }
        if (hmOrderImg.getStatus() == OrderEnum.ORDER_STOP.getIndex() - 1) {
            hmOrderImg.setType("服务结束");
        }
        iHmOrderImgService.save(hmOrderImg);
        return ResultUtil.data("图片上传成功！");
    }

    @GetMapping("changeTechnicianStatus")
    public Result<Object> changeTechnicianStatus(@RequestParam Integer status) { //1可以服务  3 服务中  4 休息中
        String id = securityUtil.getCurrentWxUser().getId();
        QueryWrapper<HmTechnician> hmTechnicianQueryWrapper = new QueryWrapper<>();
        hmTechnicianQueryWrapper.lambda().eq(HmTechnician::getWxUserId, id);
        HmTechnician hmTechnician = iHmTechnicianService.getOne(hmTechnicianQueryWrapper);
        if(hmTechnician == null) {
            return ResultUtil.error("当前技师信息获取失败！请刷新重试！");
        }

        HmTechnician technician = new HmTechnician();
        technician.setId(hmTechnician.getId());
        technician.setIsWork(status);
        boolean b = iHmTechnicianService.updateById(technician);
        if(b) {
            return ResultUtil.data("服务状态修改成功！");
        }
        return ResultUtil.error("服务状态修改失败！");
    }
}
